package edu.jamestang.yeb.exception;

import edu.jamestang.yeb.util.Resp;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author JamesTang
 * @date 2021/12/14 23:43
 */
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(SQLException.class)
    public Resp sqlException(SQLException e){
        if(e instanceof SQLIntegrityConstraintViolationException){
            return Resp.error("This data has been used, Operation fail");
        }

        return Resp.error("Database error, Operation fail");
    }

}
